package org.slusk.thynwor.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JCheckBox;
import javax.swing.JToggleButton;

import org.slusk.thynwor.corestructures.Drone;
import org.slusk.thynwor.corestructures.Environment;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import net.miginfocom.swing.MigLayout;

public class OptionsDialog extends JDialog {

	private Environment environment;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtSimSpeed;
	private JTextField txtLayoutFile;
	private JCheckBox chckbxUseRandomLayout;
	private JCheckBox chckbxUseOnlineReinforcement;
	private JLabel lblLayoutFile;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			OptionsDialog dialog = new OptionsDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public OptionsDialog() {
		setTitle("Options");
		setBounds(100, 100, 483, 315);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[124px][5px][80px,grow][261px]", "[19px][23px][23px][15px][]"));
		{
			JLabel lblSimulationSpeed = new JLabel("Simulation Speed");
			contentPanel.add(lblSimulationSpeed, "cell 0 0,alignx left,aligny center");
		}
		{
			txtSimSpeed = new JTextField();
			txtSimSpeed.setText("900");
			contentPanel.add(txtSimSpeed, "cell 2 0,growx,aligny center");
			txtSimSpeed.setColumns(10);
		}
		{
			chckbxUseOnlineReinforcement = new JCheckBox("Use Online Reinforcement");
			chckbxUseOnlineReinforcement.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					toggleUseOnlineReinforcement();
				}
			});
			contentPanel.add(chckbxUseOnlineReinforcement, "cell 0 1 3 1,alignx left,aligny center");
		}
		{
			chckbxUseRandomLayout = new JCheckBox("Use Random Layout");
			chckbxUseRandomLayout.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					toggleUseRandomLayout();
				}
			});
			chckbxUseRandomLayout.setSelected(true);
			contentPanel.add(chckbxUseRandomLayout, "cell 0 2 3 1,alignx left,aligny center");
		}
		{
			lblLayoutFile = new JLabel("Layout File");
			lblLayoutFile.setEnabled(false);
			contentPanel.add(lblLayoutFile, "cell 0 3,alignx center,aligny center");
		}
		{
			txtLayoutFile = new JTextField();
			txtLayoutFile.setEnabled(false);
			txtLayoutFile.setEditable(false);
			contentPanel.add(txtLayoutFile, "cell 2 3 2 1,growx");
			txtLayoutFile.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						updateEnvironment();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	protected void toggleUseRandomLayout() {
		if (chckbxUseRandomLayout.isSelected()) 
			chckbxUseRandomLayout.setSelected(false);
		else
			chckbxUseRandomLayout.setSelected(true);
		
		lblLayoutFile.setEnabled(!chckbxUseRandomLayout.isSelected());
		txtLayoutFile.setEnabled(!chckbxUseRandomLayout.isSelected());
	}
	
	protected void toggleUseOnlineReinforcement() {
//		if (chckbxUseOnlineReinforcement.isSelected()) 
//			chckbxUseOnlineReinforcement.setSelected(false);
//		else
//			chckbxUseOnlineReinforcement.setSelected(true);
	}

	public void display() {
		// Get settings from environment
		txtSimSpeed.setText(String.valueOf(environment.getSimSpeed()));
		txtLayoutFile.setText(String.valueOf(environment.getLayoutName()));
		chckbxUseOnlineReinforcement.setSelected(environment.isUseOnlineReinforcement());
		chckbxUseRandomLayout.setSelected(environment.isUseRandomLayout());
		lblLayoutFile.setEnabled(!environment.isUseRandomLayout());
		txtLayoutFile.setEnabled(!environment.isUseRandomLayout());
		setVisible(true);
	}

	protected void updateEnvironment() {
		environment.setSimSpeed(Integer.parseInt(txtSimSpeed.getText()));
		environment.setUseOnlineReinforcement(chckbxUseOnlineReinforcement.isSelected());
		environment.setUseRandomLayout(chckbxUseRandomLayout.isSelected());
		environment.setLayoutName(txtLayoutFile.getText());
		setVisible(false);
	}

	public void setEnvironment(Environment environment) {
		this.environment = environment;		
	}

	public JTextField getTxtSimSpeed() {
		return txtSimSpeed;
	}

	public JCheckBox getChckbxUseRandomLayout() {
		return chckbxUseRandomLayout;
	}
	public JCheckBox getChckbxUseOnlineReinforcement() {
		return chckbxUseOnlineReinforcement;
	}
	public JTextField getTxtLayoutFile() {
		return txtLayoutFile;
	}
	public JLabel getLblLayoutFile() {
		return lblLayoutFile;
	}
}
